﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Specialized;
using System.Xml;

using Common;

namespace com.globalwave.a8ext
{
    public partial class ProductPage : Common.BasePage
    {
        public ProductPage()
            : base()
        {
            entityName = "Product";
        }


        public void list()
        {

            NameValueCollection criterion = getCriterion();

            DataSet ds = ProductService.getInstance().query(criterion, pageIndex, pageSize, null);

            renderListXML(ds);
        }


        public void list_J2ME_aerzvuya032kl09khhafli3afdaer32121()
        {

            NameValueCollection criterion = getCriterion();

            log.Debug("IEMI:" + Request.Params["imei"]);

            DataSet ds = ProductService.getInstance().query4J2ME(criterion, Request.Params["imei"]);

            string result = "";
            foreach(System.Data.DataRow row in ds.Tables[0].Rows)
            {
                if (!"".Equals(result)) {
                    result += ";";
                }
                result += row["fullname"].ToString() + "|" +
                        row["usercode"].ToString() + "|" +
                        row["price1"].ToString() + "|" +
                        row["price2"].ToString() + "|" +
                        row["price3"].ToString();  
            }

            renderXML(result);
        }

        public void list4reward()
        {

            NameValueCollection criterion = getCriterion();

            DataSet ds = ProductService.getInstance().query(criterion, pageIndex, pageSize, null);

            ds.ExtendedProperties.Add("pageIndex", this.pageIndex.ToString());
            XmlDocument doc = U.toListXML(ds);
            XmlNodeList dateNodeList = doc.SelectNodes("//eff_date");
            foreach (XmlNode dateNode in dateNodeList)
            {
                if (dateNode.InnerText != null && dateNode.InnerText.Length > 10)
                {
                    dateNode.InnerText = dateNode.InnerText.Substring(0, 10);
                }
            }
            dateNodeList = doc.SelectNodes("//exp_date");
            foreach (XmlNode dateNode in dateNodeList)
            {
                if (dateNode.InnerText != null && dateNode.InnerText.Length > 10)
                {
                    dateNode.InnerText = dateNode.InnerText.Substring(0, 10);
                }
            }
            renderXML(doc.OuterXml);
        }

        /* 不可新建操作
        public void create()
        {
            NameValueCollection product = getProduct();

            int product_id =
                ProductService.getInstance().create(product);

            product.Set("product_id", product_id.ToString());

            renderSuccessCreateMessage(product);

        }
         

        public void delete()
        {
            string[] product_ids = this.getProduct_ids();

            if (product_ids == null)
            {
                ProductService.getInstance().delete(getProduct());
            }
            else
            {
                ProductService.getInstance().deleteAll(product_ids);
            }

            renderSuccessDeleteMessage();

        }
         */

        public void update()
        {

            NameValueCollection product = getProduct();

            ProductService.getInstance().update(product);


            renderSuccessUpdateMessage(product);

        }

        private NameValueCollection getCriterion()
        {

            return U.filter(Request.Params, "productCriterion")[0];
        }

        private NameValueCollection getProduct()
        {

            NameValueCollection product = U.filter(Request.Params, "product")[0];
            if ("".Equals(product.Get("pro_product_id")))
            {
                product.Set("pro_product_id", null);
            }
            return product;
        }

        private string[] getProduct_ids()
        {
            return Request.Params.GetValues("product_ids");
        }

        private static Hashtable privilegeMap = null;

        override protected Hashtable getPrivilegeMap()
        {
            if (privilegeMap == null)
            {
                privilegeMap = new Hashtable();

                privilegeMap.Add("list", (short)8101);
                privilegeMap.Add("list4reward", (short)8109);
                //privilegeMap.Add("create", (short)8102);
                privilegeMap.Add("update", (short)8103);
                //privilegeMap.Add("delete", (short)8104);
                privilegeMap.Add("list_J2ME_aerzvuya032kl09khhafli3afdaer32121", (short)-1);
            }

            return privilegeMap;
        }
    }
}
